cmake_minimum_required (VERSION 2.8)
project (Queue)

option(BUILD_32 "build 32-bit on linux/windows" OFF)
option(BUILD_64 "build 64-bit on linux/windows" OFF)

set(DEPS_DIR "${PROJECT_BINARY_DIR}/deps/")

include_directories("${PROJECT_SOURCE_DIR}")

set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-parameter")

if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND NOT CMAKE_OSX_ARCHITECTURES)
	set (CMAKE_OSX_ARCHITECTURES "i386;x86_64")
endif ()
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND BUILD_32)
	set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
endif ()
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND BUILD_64)
	set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64")
endif ()
if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
	file(DOWNLOAD "ftp://sourceware.org/pub/pthreads-win32/dll-latest/include/pthread.h" "${DEPS_DIR}/pthreads-w32/include/pthread.h")
	file(DOWNLOAD "ftp://sourceware.org/pub/pthreads-win32/dll-latest/include/sched.h" "${DEPS_DIR}/pthreads-w32/include/sched.h")
	file(DOWNLOAD "ftp://sourceware.org/pub/pthreads-win32/dll-latest/include/semaphore.h" "${DEPS_DIR}/pthreads-w32/include/semaphore.h")
	if (BUILD_64)
		file(DOWNLOAD "ftp://sourceware.org/pub/pthreads-win32/dll-latest/dll/x64/pthreadVC2.dll" "${PROJECT_BINARY_DIR}/pthreadVC2.dll")
		file(DOWNLOAD "ftp://sourceware.org/pub/pthreads-win32/dll-latest/lib/x64/pthreadVC2.lib" "${DEPS_DIR}/pthreads-w32/lib/pthreadVC2.lib")
	else ()
		file(DOWNLOAD "ftp://sourceware.org/pub/pthreads-win32/dll-latest/dll/x86/pthreadVC2.dll" "${PROJECT_BINARY_DIR}/pthreadVC2.dll")
		file(DOWNLOAD "ftp://sourceware.org/pub/pthreads-win32/dll-latest/lib/x86/pthreadVC2.lib" "${DEPS_DIR}/pthreads-w32/lib/pthreadVC2.lib")
	endif ()
	include_directories("${DEPS_DIR}/pthreads-w32/include/")
	set_source_files_properties (queue.c queue_internal.c example.c PROPERTIES LANGUAGE CXX)
else ()
	find_package (Threads)
endif ()

add_library (queue SHARED queue.c queue_internal.c)
add_library (queue_static STATIC queue.c queue_internal.c)
add_executable (queueexample example.c)
target_link_libraries (queueexample queue_static)
if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
	target_link_libraries (queue "${DEPS_DIR}/pthreads-w32/lib/pthreadVC2.lib")
	target_link_libraries (queue_static "${DEPS_DIR}/pthreads-w32/lib/pthreadVC2.lib")
else ()
	target_link_libraries (queue ${CMAKE_THREAD_LIBS_INIT})
	target_link_libraries (queue_static ${CMAKE_THREAD_LIBS_INIT})
endif ()
